/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

/**
 *
 * @author Zlobniy
 */
public class MathUtils {
    
    private static final double pi = 3.14159265358979;
    private static final double radToDeg = 180 / pi;
    
    public static Long unsToS( long uns ){
    
        if( uns > 2147483648l ){
            return uns - 4294967296l;
        }else{
            return uns;
        }
        
    }
    
    public static int getTargetDeg(int dx, int dy){
        int deg = -1;
        int adx = Math.abs(dx);
        int ady = Math.abs(dy);
        if( dx == 0 && dy < 0 ){
            deg = 0;
        }else if( dx < 0 && dy < 0 ){
            deg = (int) (Math.atan( adx / ady ) * radToDeg);
        }else if( dx < 0 && dy == 0 ){
            deg = 90;
        }else if( dx < 0 && dy > 0 ){
            deg = (int) (Math.atan(ady / adx) * radToDeg + 90);
        }else if( dx == 0 && dy > 0 ){
            deg = 180;
        }else if( dx > 0 && dy > 0 ){
            deg = (int) (Math.atan(adx / ady) * radToDeg + 180);
        }else if( dx > 0 && dy == 0 ){
            deg = 270;
        }else if( dx > 0 && dy < 0 ){
            deg = (int) (Math.atan(ady / adx) * radToDeg + 270);
        }
        return deg;
    }
    
    public static int getMyDeg( int ns, int we, int ud ){
        int grad = 0;
        
        int ans = Math.abs(ns);
        int awe = Math.abs(we);
        int aud = Math.abs(ud);
        
        if( ns > 0 && we > 0 ){
            grad = (int) (Math.atan( awe / ans) * radToDeg);
        }else if( ns < 0 && we > 0 ){
            grad = (int) (90 +  Math.atan(ans/awe) * radToDeg);
        }else if( ns < 0 && we < 0 ){
            grad = (int) (180 + Math.atan(awe/ans) * radToDeg);
        }else if( ns > 0 && we < 0 ){
            grad = (int) (270 + Math.atan(ans/awe) * radToDeg);
        }else{
            grad = 0;
        }
        return grad;
    }
    
    public static int degToClock( int deg ){
        int clock = Math.round(deg / 30);
        if( clock == 0 ){
            clock = 12;
        }
        return clock;
    }
    
}
